﻿@model UserViewModel
@{
    ViewBag.Title = "AddUser";
}

@using (Html.BeginForm("AddUser", "Home", FormMethod.Post))
{ 
    <h2>Новый пользователь</h2>

    <table border="0">
        <tr>
            <td>Имя:</td>
            <td>
                @Html.TextBoxFor(model => model.Name)
            </td>
        </tr>

        <tr>
            <td>Емаил:</td>
            <td>
                @Html.TextBoxFor(model => model.Email)
            </td>
        </tr>

        <tr>
            <td>Иконка:</td>
            <td>
                @Html.TextBoxFor(model => model.Avatar)
            </td>
        </tr>

        <tr>
            <td>Промо код:</td>
            <td>
                @Html.TextBoxFor(model => model.Password)
                <img src="~/Images/GenerateKeys.png" height="25" style="vertical-align: middle; padding-bottom: 5px; cursor: pointer;" title="Сгенерировать новый код" onclick="generateCod(13,'pass')" />
            </td>
        </tr>

        <tr>
            <td>Именная ссылка:</td>
            <td>
                @Html.TextBoxFor(model => model.UrlId)
                <img src="~/Images/GenerateKeys.png" height="25" style="vertical-align: middle; padding-bottom: 5px; cursor: pointer;" title="Сгенерировать код для ссылки" onclick="generateCod(4, 'url')" />
            </td>
        </tr>
    </table>
    
    <div>
        <input type="submit" value="Создать" />
        <input type="button" value="Отмнить" onclick="document.location = '/Home/Admin'" />
    </div>
   
    if (Model.ValidateError)
    { 
    <div style="color: red;">
        Заплните все поля!
    </div>
    }

}

<script>
    function generateCod(length, type) {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Home/GenaretCod",
            data: JSON.stringify({ length: length }),
            success: function (response) {
                if (type == "pass")
                    $('#Password').val(response);
                else
                    $('#UrlId').val(response);
            }
        });
    }
</script>
